package com.dinggou.shopjb.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import java.util.Map;

public class ActivityUtil {
    public static boolean isActivityFinishingOrFinished(Activity activity) {
        if (activity == null) return true;
        if (activity.isFinishing()) return true;
        if (activity.isDestroyed()) return true;
        return false;
    }

    public static boolean isActivityFinishingOrFinished(Fragment fragment) {
        if (fragment == null) return true;
        Activity activity = fragment.getActivity();
        if (activity == null) return true;
        if (activity.isFinishing()) return true;
        if (activity.isDestroyed()) return true;
        return false;
    }

    public static boolean isActivityFinishingOrFinished(Context context) {
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            return isActivityFinishingOrFinished(activity);
        }
        return false;
    }

    public static ActivityResultLauncher<String[]> registerForActivityPermissionsResult(AppCompatActivity activity, ActivityResultCallback<Map<String, Boolean>> callback) {
        if (activity == null) return null;
        return activity.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), callback);
    }

    public static ActivityResultLauncher<String> registerForActivityPermissionResult(AppCompatActivity activity, ActivityResultCallback<Boolean> callback) {
        if (activity == null) return null;
        return activity.registerForActivityResult(new ActivityResultContracts.RequestPermission(), callback);
    }

    public static ActivityResultLauncher<IntentSenderRequest> registerForActivityDataResult(AppCompatActivity activity, ActivityResultCallback<ActivityResult> callback) {
        if (activity == null) return null;
        return activity.registerForActivityResult(new ActivityResultContracts.StartIntentSenderForResult(), callback);
    }

    public static void finishActivity(Activity activity, int resultCode, Intent resultIntent) {
        if (ActivityUtil.isActivityFinishingOrFinished(activity)) return;
        activity.setResult(resultCode, resultIntent);
        activity.finish();
    }

    public static void finishActivity(Activity activity) {
        if (ActivityUtil.isActivityFinishingOrFinished(activity)) return;
        activity.finish();
    }

    public static void sendBroadcast(Activity activity, Intent intent) {
        if (ActivityUtil.isActivityFinishingOrFinished(activity)) return;
        if (intent == null) return;
        activity.sendBroadcast(intent);
    }
}
